package com.hevs.samplewebapp.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;

import com.hevs.samplewebapp.client.bean.Type;
import com.hevs.samplewebapp.client.screens.AddMedicalDataWidget;
import com.hevs.samplewebapp.client.screens.GrantRevokeDoctorWidget;
import com.hevs.samplewebapp.client.screens.ManageMedicalDataWidget;
import com.hevs.samplewebapp.client.screens.PatientGraphWidget;
import com.hevs.samplewebapp.client.screens.StatisticsWidget;

/**
 * This is the Patient Panel
 * <p>The Goal is to provide the patient different tools to manage its data. 
 * The patient will be able to manage its data, display statistics, graphical representation and granting/revoking doctor.</p> 
 * @author Lionel Rey
 * @date 12.03.2012 
 */
public class Patient extends Composite{
	
	// ------------------------------------------------------------
	// VARIABLES
	// ------------------------------------------------------------

	// Graphical Elements
	
	/**
	 * Horizontal panel
	 * <p>Panel contains a TabPanel to show different widgets.</p>
	 */
	HorizontalPanel hp = new HorizontalPanel();

	/**
	 * Patient panel
	 * <p>Panel with multiple tabs, to make easy the adding of new features for administration.</p>
	 */
	private TabPanel tpDisplayData = new TabPanel();

	/**
	 * Managing data tool
	 * <p>Widget that contents tools to manage the medical data of the patient.</p>
	 * @see ManageMedicalDataWidget
	 */
	private ManageMedicalDataWidget manageMedicaldataWidget = new ManageMedicalDataWidget();
	
	/**
	 * Adding data tool
	 * <p>Widget that contents tools to add new medical data</p>
	 * @see AddMedicalDataWidget
	 */
	private AddMedicalDataWidget addMedicalDataWidget = new AddMedicalDataWidget();
	
	/**
	 * Granting/Revoking doctor tool
	 * <p>Widget that contents tools to grant or revoke a doctor</p>
	 * @see GrantRevokeDoctorWidget
	 */
	private GrantRevokeDoctorWidget grantRevokeDoctorWidget= new GrantRevokeDoctorWidget();
	
	/**
	 * Statistics tool
	 * <p>Widget that contents tools to display different statistics</p>
	 * @see StatisticsWidget
	 */
	private StatisticsWidget statisticsWidget = new StatisticsWidget();
	
	/**
	 * Graphical representation tool
	 * <p>Widget that contents tools to display graphical representation of the data</p>
	 * @see PatientGraphWidget
	 */
	private PatientGraphWidget graphWidget=new PatientGraphWidget();

	// URL
	/**
	 * Web service URL
	 * <p>URL for getting, adding and updating medical records
	 */
	public static final String JSON_URL_MEDICALDATA = GWT.getModuleBaseURL() + "ws/ressources/medicalrecords";
	/**
	 * Web service URL
	 * <p>URL for deleting medical records
	 */
	public static final String JSON_URL_MEDICALDATA_DEL = GWT.getModuleBaseURL() + "ws/delete/medicalrecords";
	
	/**
	 * Web service URL
	 * <p>URL for granting a doctor</p>
	 */
	public static final String JSON_URL_GRANT = GWT.getModuleBaseURL() + "ws/granting/users";
	/**
	 * Web service URL
	 * <p>URL for revoking a doctor</p>
	 */
	public static final String JSON_URL_REVOKE = GWT.getModuleBaseURL() + "ws/revoking/users";

	// ----------------------------------------------------------------------------
	// Constructor
	// ----------------------------------------------------------------------------

	/**
	 * Constructor of the patient tools
	 * <p>it calls the method to build all the tab and a method to refresh data</p>
	 * @see ManageMedicalDataWidget#refreshComp()
	 * @see Patient#buildWidget()
	 */
	public Patient (){
		buildWidget();
		
		// refresh MedicalData Widget
		manageMedicaldataWidget.refreshComp();

		initWidget(hp);
	}

	// ----------------------------------------------------------------------------
	// Methods
	// ----------------------------------------------------------------------------
	/**
	 * Method to get the medical data tab to display
	 * @return a TabPanel component
	 */
	public TabPanel getTpDisplayData() {
		return tpDisplayData;
	}

	/**
	 * Method to build the widget
	 * <p> it adds different tab 
	 * <ul>
	 * <li> a tab to add new medical data</li>
	 * <li> a tab to display and manage medical data</li>
	 * <li> a tab to display statistics</li>
	 * <li> a tab to display graphical representation of data</li>
	 * <li> a tab to grant/revoke doctor and display them</li>
	 * </ul>
	 * </p>
	 */
	private void buildWidget() {
		// Set alignment in the panel
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		// Tabs of the panel
		tpDisplayData.add( addMedicalDataWidget, "Add new data");
		tpDisplayData.add( manageMedicaldataWidget, "Manage data");
		tpDisplayData.add( statisticsWidget, "Statistics");
		tpDisplayData.add( graphWidget, "Graphs");
		tpDisplayData.add( grantRevokeDoctorWidget, "Add/Revoke doctor");
		
		// Handler to refresh each tab on click
		tpDisplayData.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> tab) {
				// refresh Manage data tab
				if(tab.getSelectedItem()==1)
					manageMedicaldataWidget.refreshComp();
				// refresh statistics tab
				if(tab.getSelectedItem()==2)
					statisticsWidget.refresh();
				// refresh graph tab
				if(tab.getSelectedItem()==3)
				{
					graphWidget.getData();
					graphWidget.getMedDatList();
					graphWidget.refreshGraph(Type.getFromInt(0));
				}
				// refresh grant revoke tab
				if(tab.getSelectedItem()== 4)
					grantRevokeDoctorWidget.refreshComp();
			}
		});
		
		tpDisplayData.selectTab(0);
		// Adding the tab panel to the principal panel
		hp.add(tpDisplayData);
	}
}